import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者  Administrator
 * 时间  2017/5/27 16:45
 * 文件  SqliteDemo
 * 描述  读取Excel 文件
 */
public class ReadExcel {


	public static List<List<String>> parseExcel(String name) throws IOException {
      InputStream inputStream= new FileInputStream(name);

		HSSFWorkbook hssfWorkbook=new HSSFWorkbook(inputStream);

		List<List<String>> result = new ArrayList<List<String>>();// 每行

		for (int i = 0; i < hssfWorkbook.getNumberOfSheets(); i++) {
			HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(i);
			if (hssfSheet == null||hssfSheet.getLastRowNum()==0) {
				continue;
			}
			System.out.println("最后一行："+hssfSheet.getLastRowNum());
			for (int j = 0; j <= hssfSheet.getLastRowNum(); j++) {
				HSSFRow hssfRow = hssfSheet.getRow(j);// 当前行
				int minColIx = hssfRow.getFirstCellNum();
				int maxColIx = hssfRow.getLastCellNum();
				List<String> rowList = new ArrayList<String>();// 每个单元格

				for (int k = minColIx; k < maxColIx; k++) {
					HSSFCell cell = hssfRow.getCell(k);
					if (cell == null) {
						continue;
					}
					rowList.add(getStringVal(cell));
				}
				result.add(rowList);
			}

		}

		return result;
	}



	public static String getStringVal(HSSFCell cell){
		switch (cell.getCellType()){
			case Cell.CELL_TYPE_BOOLEAN:
				return cell.getBooleanCellValue()?"TRUE":"FALSE";
			case Cell.CELL_TYPE_FORMULA:
				return cell.getCellFormula();
			case Cell.CELL_TYPE_NUMERIC:
				cell.setCellType(Cell.CELL_TYPE_STRING);
				return cell.getStringCellValue();
			case Cell.CELL_TYPE_STRING:
				return cell.getStringCellValue();
			default:
				return "0";
		}
	}

}
